/*
 * @Author: luoqi
 * @Date: 2022-03-28 18:48:13
 * @LastEditors: luoqi
 * @LastEditTime: 2022-04-27 16:38:29
 * @Description: 
 */
import React, { useState,useEffect } from 'react';
import { Form, Input, Button, Select, Space, Card, Table, Tag } from 'antd';
import { SearchOutlined, PlusOutlined } from '@ant-design/icons';
import ModifyClassify from './ModifyClassify';
import axios from '../../utils/axios';
import qs from 'qs'
const { Option } = Select;
const columns = [
  {
    title: '分类名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: 'icon图片',
    dataIndex: 'iconImg',
    key: 'iconImg',
    render: text => <img src={text} style={{width:'50px',height:'50px'}} alt="" />
  },
  {
    title: '操作',
    key: 'action',
    render: (text, record) => (
      <Space size="middle">
        <ModifyClassify data={record}/>
        <Button type="primary" ghost>删除</Button>
      </Space>
    ),
  },
];

const Parentclassify = () => {
  let [pag, setPag] = useState({ defaultCurrent: 1,  pageSize: 5 ,current:1,showTotal:total => `共 ${total} 条`,onChange:changePage})
  let [data,setData]=useState([])
  let [type,setType]=useState(0)
  //页数更改时
  function changePage(page,pageSize){
    setPag({...pag,current:page,pageSize})
  }
  useEffect(()=>{
    axios({
      url:'/classify/selectAll',
      method:'post',
      data:qs.stringify({type:type,pid:0,page:pag.current})
    }).then(res=>{
      console.log(res);
      if(res.code=='200'){
        //console.log(res.data);
        setData(res.data.map(item=>({...item,key:item.id}))) 
        setPag({...pag,total:res.count})
      }
    })
  },[type,pag.current])
  const onFinishType=(values)=>{
    console.log('Success:', values);
  }
  function handleChange(value) {
    console.log(`selected ${value}`);
    setType(value)
  }
  return (
    <div>
      <Form
            name="basic"
            labelCol={{
              span: 8,
            }}
            wrapperCol={{
              span: 16,
            }}
            initialValues={{
              remember: true,
              type:type
            }}
            onFinish={onFinishType}
            autoComplete="off"
          >
            <Space>
              <Form.Item
                label="类型"
                name="type"
              >
                <Select value={type}  style={{ width: 120 }} onChange={handleChange}>
                  <Option value={0}>按部位</Option>
                  <Option value={1}>按科室</Option>
                </Select>
              </Form.Item>
              
            </Space>
          </Form>
          <Table columns={columns} dataSource={data} pagination={pag} />
    </div>
  );
}

export default Parentclassify;
